package com.dhcc.DRGsHosp.facade.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.facade.YwcHomeFacade;
import com.dhcc.DRGsHosp.facade.dto.IndexCalDTO;
import com.dhcc.DRGsHosp.infra.util.Arith;
import com.dhcc.DRGsHosp.infra.util.DrgsIndex;
import com.dhcc.DRGsHosp.infra.util.MapComparatorImpl;

/**
 * @title		:MysqlHomeFacadeImpl.java
 * @description	:
 * @author		:lileizheng
 * @date		:2015年10月22日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014 
 */
@Named("drgsHospFacadeImpl51")
public class YwcHomeFacadeImpl implements YwcHomeFacade {
	@Autowired
	private DrgsIndexCalFacadeImpl  drgsIndexCalFacadeImpl;

	@Override
	public InvokeResult getDepTotalScore(IndexCalDTO dto) {
		List<Map<String, Object>> mlist =  drgsIndexCalFacadeImpl.indexCal(dto);
		Collections.sort(mlist, new MapComparatorImpl("QuotaValuecc", "desc"));
		Map<String, Object> lsMap = new HashMap<>();
		for(int i=0; i<mlist.size(); i++){
			Map<String, Object> map = mlist.get(i);
			lsMap.put(map.get("ccode")!=null?map.get("ccode").toString():"null", i+1);
		}
		Collections.sort(mlist, new MapComparatorImpl("QuotaValue", "desc"));
		for(int i=0; i<mlist.size(); i++){
			Map<String, Object> map = mlist.get(i);
			map.put("index", i+1);
			map.put("indexcc", lsMap.get(map.get("ccode")!=null?map.get("ccode").toString():"null"));
		}
		return InvokeResult.success(mlist);
	}
	/**
	 * 
	 *  @Description：医政首页全院指标展示
	 *  @Author LiuHongJun  
	 *  @see com.dhcc.DRGsHosp.facade.YwcHomeFacade#getCMIDRGCount(com.dhcc.DRGsHosp.facade.dto.IndexCalDTO)
	 */
	public InvokeResult getCMIDRGCount(IndexCalDTO dto){
		List<Map<String, Object>> resultList = new LinkedList<>();
		String dateFrom = DrgsIndex.formateDateFrom(dto.getDateFrom());
		String dateTo = DrgsIndex.formateDateTo(dto.getDateTo());
		String[] colors = {"#c1232b","#27727b","#fcce10","#e87c25","#b5c334","#fe8463","#9bca63","#fad860","#f3a43b","#60c0dd","#d7504b","#c6e579"};
		if("MC".equals(dto.getMrType())){
			int yearFrom = Integer.parseInt(dateFrom.substring(0, 4));
			int yearTo = Integer.parseInt(dateTo.substring(0, 4));
			for(int i=4; i>=0; i--){
				String reldateFrom = (yearFrom-i)+"-"+dateFrom.substring(5,7);
				String reldateTo = (yearTo-i)+"-"+dateTo.substring(5,7);
				dto.setDateFrom(reldateFrom);
				dto.setDateTo(reldateTo);
				List<Map<String, Object>> mlist =  drgsIndexCalFacadeImpl.indexCal(dto);
				Map<String, Object> map = new HashMap<String, Object>();
				if(mlist.size()>0){
					map = mlist.get(0);	
				}
				if(yearFrom!=yearTo){
					map.put("ddesc", (yearFrom-i)+"~"+(yearTo-i)+"年");
				}else{
					map.put("ddesc", (yearFrom-i)+"年");
				}
				map.put("shape", "diamond");
				map.put("color", colors[i]);
				map.put("size", 5);
				resultList.add(map);
			}
		}else if("M".equals(dto.getMrType())){
			String mrYear = dateTo.substring(0, 4);
			for(int i=1; i<13; i++){
				int year = Integer.parseInt(mrYear);
				if(i<10){
					String reldateFrom =year+"-0"+i;
					String reldateTo = year+"-0"+i;
					dto.setDateFrom(reldateFrom);
					dto.setDateTo(reldateTo);
				}else{
					String reldateFrom =year+"-"+i;
					String reldateTo = year+"-"+i;
					dto.setDateFrom(reldateFrom);
					dto.setDateTo(reldateTo);
				}
				List<Map<String, Object>> mlist =  drgsIndexCalFacadeImpl.indexCal(dto);
				if(mlist.size()>0){
					Map<String, Object> map = mlist.get(0);
					map.put("ddesc", i+"月");
					map.put("shape", "diamond");
					map.put("color", colors[i-1]);
					map.put("size", 5);
					resultList.add(map);
				}else{
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("ddesc", i+"月");
					map.put("shape", "diamond");
					map.put("color", colors[i-1]);
					map.put("size", 5);
					resultList.add(map);
				}
			}
		}else if("custom".equals(dto.getMrType())){
			//自定义时求累计值
			int yearFrom = Integer.parseInt(dateFrom.substring(0, 4));
			int yearTo = Integer.parseInt(dateTo.substring(0, 4));
			for(int m=0; m<5;m++){
				String reldateFrom = (yearFrom-m)+"-"+dateFrom.substring(5,7);
				String reldateTo = (yearTo-m)+"-"+dateTo.substring(5,7);
				dto.setDateFrom(reldateFrom);
				dto.setDateTo(reldateTo);
				List<Map<String, Object>> mlist =  drgsIndexCalFacadeImpl.indexCal(dto);
				Map<String, Object> map = new HashMap<String, Object>();
				if(mlist.size()>0){
					map = mlist.get(0);
				}
				map.put("ddesc", reldateFrom.replace("-",".")+"~"+reldateTo.replace("-","."));
				map.put("shape", "diamond");
				map.put("color", colors[m]);
				map.put("size", 5);
				resultList.add(map);
		}
		}
		Collections.sort(resultList, new MapComparatorImpl("ddesc", "asc"));
		return InvokeResult.success(resultList);
	}
/**
 * 
 *  @Description：病组指标象限分析
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.YwcHomeFacade#getDoubleQuotaAnalysis(com.dhcc.DRGsHosp.facade.dto.IndexCalDTO)
 */
	@Override
	public InvokeResult getDoubleQuotaAnalysis(IndexCalDTO dto) {
		List<Map<String, Object>> resultList = new LinkedList<Map<String,Object>>();
		List<Map<String, Object>> mlist =   drgsIndexCalFacadeImpl.indexCal(dto);
		for(Map<String, Object> map: mlist){
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("ccode", map.get("ccode"));
			result.put("ddesc", map.get("ddesc"));
			//没有比较值的时候不画图
			if(map.get("AvgDayscc")!=null){
				double value = map.get("AvgDays")!=null?Double.parseDouble(map.get("AvgDays").toString()):0;
				double valuecc =Double.parseDouble(map.get("AvgDayscc").toString());
				double valuesub = value - valuecc;
				result.put("AvgDays", Arith.round(valuesub,2));
			}
			if(map.get("AvgFeecc")!=null){
				double value1 = map.get("AvgFee")!=null?Double.parseDouble(map.get("AvgFee").toString()):0;
				double value1cc = Double.parseDouble(map.get("AvgFeecc").toString());
				double value1sub = value1 - value1cc;
				result.put("AvgFee", Arith.round(value1sub, 2));
			}
			if(map.get("DrugRatiocc")!=null){
				double value2 = map.get("DrugRatio")!=null?Double.parseDouble(map.get("DrugRatio").toString()):0;
				double value2cc = Double.parseDouble(map.get("DrugRatiocc").toString());
				double value2sub = value2 - value2cc;
				result.put("DrugRatio", Arith.round(value2sub,2));
			}
			resultList.add(result);
		}
		return InvokeResult.success(resultList);
	}
	/**
	 * 
	 *  @Description：按时间段查询指标
	 *  @Author LiuHongJun  
	 *  @see com.dhcc.DRGsHosp.facade.YwcHomeFacade#getYwcHospQuota(com.dhcc.DRGsHosp.facade.dto.IndexCalDTO)
	 */
	@Override
	public InvokeResult getYwcHospQuota(IndexCalDTO dto) {
		List<Map<String, Object>> resultList = new LinkedList<>();
		String dateFrom = DrgsIndex.formateDateFrom(dto.getDateFrom());
		String dateTo = DrgsIndex.formateDateTo(dto.getDateTo());
		String[] colors = {"#c1232b","#27727b","#fcce10","#e87c25","#b5c334","#fe8463","#9bca63","#fad860","#f3a43b","#60c0dd","#d7504b","#c6e579"};
		
		if("MC".equals(dto.getMrType())){
			int yearFrom = Integer.parseInt(dateFrom.substring(0, 4));
			dto.setDateFrom((yearFrom-4)+"-"+dateFrom.substring(5,7));
			List<Map<String, Object>> mlist =drgsIndexCalFacadeImpl.indexCal(dto);
			if(!mlist.isEmpty()){
				int i=0;
				for(Map<String, Object> map:mlist){
					map.put("ddesc", map.get("ddesc")+"年");
					map.put("shape", "diamond");
					map.put("color", colors[i]);
					map.put("size", 5);
					resultList.add(map);
					i++;
				}
			}
		}else if("M".equals(dto.getMrType())){
			String mrYear = dateTo.substring(0, 4);
			for(int i=1; i<13; i++){
				int year = Integer.parseInt(mrYear);
				if(i<10){
					String reldateFrom =year+"-0"+i;
					String reldateTo = year+"-0"+i;
					dto.setDateFrom(reldateFrom);
					dto.setDateTo(reldateTo);
				}else{
					String reldateFrom =year+"-"+i;
					String reldateTo = year+"-"+i;
					dto.setDateFrom(reldateFrom);
					dto.setDateTo(reldateTo);
				}
				List<Map<String, Object>> mlist =  drgsIndexCalFacadeImpl.indexCal(dto);
				if(mlist.size()>0){
					Map<String, Object> map = mlist.get(0);
					map.put("ddesc", i+"月");
					map.put("shape", "diamond");
					map.put("color", colors[i-1]);
					map.put("size", 5);
					resultList.add(map);
				}else{
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("ddesc", i+"月");
					map.put("shape", "diamond");
					map.put("color", colors[i-1]);
					map.put("size", 5);
					resultList.add(map);
				}
			}
		}else if("custom".equals(dto.getMrType())){
			//自定义时求累计值
			int yearFrom = Integer.parseInt(dateFrom.substring(0, 4));
			int yearTo = Integer.parseInt(dateTo.substring(0, 4));
			for(int m=0; m<5;m++){
				String reldateFrom = (yearFrom-m)+"-"+dateFrom.substring(5,7);
				String reldateTo = (yearTo-m)+"-"+dateTo.substring(5,7);
				dto.setDateFrom(reldateFrom);
				dto.setDateTo(reldateTo);
				List<Map<String, Object>> mlist = drgsIndexCalFacadeImpl.indexCal(dto);
				Map<String, Object> map = new HashMap<String, Object>();
				if(mlist.size()>0){
					map = mlist.get(0);
					
				}
				map.put("ddesc", reldateFrom+"~"+reldateTo);
				map.put("shape", "diamond");
				map.put("color", colors[m]);
				map.put("size", 5);
				resultList.add(map);
		}
		}
		Collections.sort(resultList, new MapComparatorImpl("ddesc", "asc"));
		return InvokeResult.success(resultList);
	}
	/**
	 * 
	 *  @Description：获取分组年份
	 *  @Author LiuHongJun  
	 *  @see com.dhcc.DRGsHosp.facade.YwcHomeFacade#getMrDate(com.dhcc.DRGsHosp.facade.dto.IndexCalDTO)
	 */
	@Override
	public InvokeResult getMrDate(IndexCalDTO dto) {
		return InvokeResult.success(drgsIndexCalFacadeImpl.getMrDate());
	}
	
}
